﻿using System;
using System.Text;


namespace Bpms.Common
{

    /// <summary>
    /// 服务器端验证
    /// </summary>
    public class VerifyHelper
    {
        /// <summary>
        /// 启动字符串格式相关验证
        /// </summary>
        /// <param name="text">要验证内容</param>
        /// <returns></returns>
        public static VerifyString StartVerify(string text)
        {
            return new VerifyString(text);
        }
    }

    /// <summary>
    /// 验证相关信息
    /// </summary>
    public class VerifyInfo
    {
        private bool _status;
        private string _message;

        /// <summary>
        /// 设置状态， true 没错误，false 有错误
        /// </summary>
        public bool Status
        {
            get
            {
                return this._status;
            }
            set
            {
                this._status = value;
            }
        }

        /// <summary>
        /// 设置信息
        /// </summary>
        public string Message
        {
            get
            {
                return this._message;
            }
            set
            {
                this._message = value;
            }
        }
    }

    public class Verify
    {
        protected bool status;
        protected string message;

        public VerifyInfo EndVerify()
        {
            VerifyInfo verifyInfo = new VerifyInfo();
            verifyInfo.Status = status;
            verifyInfo.Message = message;

            return verifyInfo;
        }
    }

    /// <summary>
    /// 字符串格式相关验证
    /// </summary>
    public class VerifyString : Verify
    {
        string text = null;

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="stringValue">要验证的字符串</param>
        public VerifyString(string stringValue)
        {
            text = stringValue;
            status = true;
            message = string.Empty;
        }

        /// <summary>
        /// 是否为null或空
        /// </summary>
        /// <param name="msg">提示信息</param>
        /// <returns></returns>
        public VerifyString IsNotNullOrEmpty(string msg)
        {
            if (string.IsNullOrEmpty(text))
            {
                status = false;
                message = msg;
            }

            return this;
        }

        /// <summary>
        /// 最小长度
        /// </summary>
        /// <param name="minLength">比较长度数值</param>
        /// <param name="msg">提示信息</param>
        /// <returns></returns>
        public VerifyString MinLength(int minLength, string msg)
        {
            status = Utility.GetStringLength(text) < minLength;
            if (!status)
            {
                message = msg;
            }

            return this;
        }

        /// <summary>
        /// 最大长度
        /// </summary>
        /// <param name="maxLength">比较长度数值</param>
        /// <param name="msg">提示信息</param>
        /// <returns></returns>
        public VerifyString MaxLength(int maxLength, string msg)
        {
            status = Utility.GetStringLength(text) > maxLength;
            if (!status)
            {
                message = msg;
            }

            return this;
        }

        /// <summary>
        /// 是否为整型
        /// </summary>
        /// <param name="msg">提示信息</param>
        /// <returns></returns>
        public VerifyString IsInt(string msg)
        {
            if (!status)
            {
                return this;
            }
            status = Validity.IsValidInt(text);
            if (!status)
            {
                message = msg;
            }

            return this;
        }

        /// <summary>
        /// 最小范围
        /// </summary>
        /// <param name="min">比较的数值</param>
        /// <param name="msg">提示信息</param>
        /// <returns></returns>
        public VerifyString Min(int min, string msg)
        {
            if (!status)
            {
                return this;
            }
            if (int.Parse(text) < min)
            {
                status = false;
                message = msg;
            }

            return this;
        }

        /// <summary>
        /// 最大范围
        /// </summary>
        /// <param name="max">比较的数值</param>
        /// <param name="msg">提示信息</param>
        /// <returns></returns>
        public VerifyString Max(int max, string msg)
        {
            if (!status)
            {
                return this;
            }

            if (int.Parse(text) > max)
            {
                status = false;
                message = msg;
            }

            return this;
        }

        /// <summary>
        /// 是否为有效的Email
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public VerifyString IsEmail(string msg)
        {
            if (!status)
            {
                return this;
            }


            status = Validity.IsValidEmail(text);

            if (!status)
            {
                message = msg;
            }

            return this;
        }
    }


    /// <summary>
    /// 数字格式相关验证
    /// </summary>
    public class VerifyInt : Verify
    {

    }



}
